<template>
  <div class="app-container">
    <!-- 查询条件开始 -->
    <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="项目名称" prop="checkItemName">
        <el-input
          v-model="queryParams.checkItemName"
          placeholder="请输入项目名称"
          clearable
          size="small"
          style="width:240px"
        />
      </el-form-item>
      <el-form-item label="关键字" prop="keywords">
        <el-input
          v-model="queryParams.keywords"
          placeholder="请输入关键字"
          clearable
          size="small"
          style="width:240px"
        />
      </el-form-item>
      <el-form-item label="项目类型" prop="typeId">
        <el-select
          v-model="queryParams.typeId"
          placeholder="项目类型"
          clearable
          size="small"
          style="width:240px"
        >
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="可用状态"
          clearable
          size="small"
          style="width:240px"
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="primary" icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 查询条件结束 -->
    <!-- 表格工具按钮开始 -->
    <el-row :gutter="10" style="margin-bottom: 8px;">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete">批量删除</el-button>
      </el-col>
    </el-row>
    <!-- 表格工具按钮结束 -->
    <!-- 数据表格开始 -->
    <el-table v-loading="loading" border :data="checkItemTableList" @selection-change="handleSelectionChnage">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="项目ID" align="center" prop="checkItemId" />
      <el-table-column label="项目名称" align="center" prop="checkItemName" />
      <el-table-column label="关键字" align="center" prop="keywords" />
      <el-table-column label="项目单价" align="center" prop="unitPrice" />
      <el-table-column label="项目成本" align="center" prop="cost" />
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="项目类别" align="center" prop="typeId" :formatter="typeFormatter" />
      <el-table-column label="状态" prop="status" align="center" :formatter="statusFormatter" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" icon="el-icon-edit" size="mini" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button type="text" icon="el-icon-delete" size="mini" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 数据表格结束 -->
    <!-- 分页插件开始 -->
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getcheckItemTableList" />
    <!-- 分页插件开始 -->

    <!-- 添加修改弹出层开始 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="500px"
      center
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="项目类型" prop="typeId">
          <el-radio-group v-model="form.typeId">
            <el-radio
              v-for="dict in typeOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
              :value="dict.dictValue"
            >{{ dict.dictLabel }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="项目名称" prop="checkItemName">
          <el-input v-model="form.checkItemName" placeholder="请输入项目名称" clearable size="small" />
        </el-form-item>
        <el-form-item label="关键字" prop="keywords">
          <el-input v-model="form.keywords" placeholder="请输入关键字" clearable size="small" />
        </el-form-item>
        <el-form-item label="项目价格" prop="unitPrice">
          <el-input-number v-model="form.unitPrice" placeholder="请输入项目价格" clearable size="mini" />
        </el-form-item>
        <el-form-item label="项目成本" prop="cost">
          <el-input-number v-model="form.cost" placeholder="请输入项目成本" clearable size="mini" />
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入单位" clearable size="small" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
              :value="dict.dictValue"
            >{{ dict.dictLabel }}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </span>
    </el-dialog>
    <!-- 添加修改弹出层结束 -->

  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import { listCheckItemForPage, addCheckItem, updateCheckItem, getCheckItemById, deleteCheckItemByIds } from '@/api/system/checkItem/checkItem'
import vPinyin from '@/utils/vue-py'
export default {
  components: {
    Pagination
  },
  // 定义页面数据
  data() {
    return {
      // 是否启用遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 分页数据总条数
      total: 0,
      // 字典表格数据
      checkItemTableList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 状态数据字典
      statusOptions: [],
      // 项目类型数据字典
      typeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        checkItemName: undefined,
        keywords: undefined,
        typeId: undefined,
        status: undefined
      },
      // 表单数据
      form: {},
      // 翻译首字母和缩写
      addTrafficHub: {},
      // 表单校验
      rules: {
        checkItemName: [
          { required: true, message: '项目名称不能为空', trigger: 'blur' }
        ],
        keywords: [
          { required: true, message: '关键字不能为空', trigger: 'blur' }
        ],
        unitPrice: [
          { required: true, message: '项目价格不能为空', trigger: 'blur' }
        ],
        cost: [
          { required: true, message: '项目成本不能为空', trigger: 'blur' }
        ],
        unit: [
          { required: true, message: '项目单位不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  // 监听器
  watch: {
    // 翻译首字母和缩写
    'form.checkItemName': function() {
      if (this.form.checkItemName) {
        this.addTrafficHub.pinYin = vPinyin.chineseToPinYin(this.form.checkItemName)
        let SX = ''
        for (var i = 0; i < this.addTrafficHub.pinYin.length; i++) {
          var c = this.addTrafficHub.pinYin.charAt(i)
          if (/^[A-Z]+$/.test(c)) {
            SX += c
          }
        }
        // 注入首字母
        this.form.keywords = SX
      }
    }
  },
  created() {
    // 加载字典状态
    // 使用全局的根据字典类型查询字典数据的方法查询字典数据
    this.getDataByType('sys_normal_disable').then(res => {
      this.statusOptions = res.data
    })

    // 使用全局的根据字典类型查询字典数据的方法查询字典数据
    this.getDataByType('his_inspection_type').then(res => {
      this.typeOptions = res.data
      console.log(this.typeOptions)
    })

    // 加载检查项目条件
    this.getcheckItemTableList()
  },
  methods: {
    // 查询检查项目日志信息
    getcheckItemTableList() {
      this.loading = true
      listCheckItemForPage((this.queryParams)).then(res => {
        this.checkItemTableList = res.data
        this.total = res.total
        this.loading = false
      })
    },
    // 模糊查询
    handleQuery() {
      this.getcheckItemTableList()
    },
    // 重置模糊查询列表
    resetQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.getcheckItemTableList()
    },
    // 重置表单
    reset() {
      this.resetForm('form')
      this.form = {
        checkItemId: undefined,
        checkItemName: undefined,
        checkItemLeader: undefined,
        checkItemNumber: undefined,
        orderNum: 0,
        regNumber: 0,
        typeId: '1',
        status: '0'
      }
    },
    // 项目类别翻译
    typeFormatter(row) {
      return this.selectDictLabel(this.typeOptions, row.typeId)
    },
    // 状态翻译
    statusFormatter(row) {
      return this.selectDictLabel(this.statusOptions, row.status)
    },
    // 监听多选框 数据表格的多选择框选择时触发
    handleSelectionChnage(selection) {
      // 记录选中的ID/删除
      this.ids = selection.map(item => item.checkItemId)
      // 修改 如果选中数大于1
      this.single = selection.length !== 1
      // 删除 如果选中的数不为空 false/true
      this.multiple = !selection.length
    },
    // 添加检查项目
    handleAdd() {
      // 打开
      this.open = true
      this.title = '添加检查项目资料'
      this.reset()
    },

    // 修改检查项目
    handleUpdate(row) {
      // 打开
      this.open = true
      this.title = '修改检查项目资料'
      this.reset()
      // 根据当前行的ID 查询 检查项目资料
      const id = row.checkItemId || this.ids
      getCheckItemById(id).then(res => {
        this.form = res.data
      })
    },
    // 删除检查项目
    handleDelete(row) {
      const dictCodes = row.checkItemId || this.ids
      this.$confirm('此操作将永久删除该字典数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true
        deleteCheckItemByIds(dictCodes).then(res => {
          this.loading = false
          this.msgSuccess('删除成功')
          this.reset()
          this.getcheckItemTableList()// 全查询
        })
      }).catch(() => {
        this.msgError('删除已取消')
        this.loading = false
      })
    },
    // 保存添加、修改
    handleSubmit() {
      this.loading = true
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.checkItemId === undefined) {
            addCheckItem(this.form).then(res => {
              // 提示
              if (res.code === 200) this.msgSuccess(res.msg)
              else this.msgError(res.msg)
              // 刷新一下下
              this.getcheckItemTableList()
              // 关闭加载遮罩
              this.loading = false
              // 关闭遮罩
              this.open = false
            }).catch(() => {
              this.loading = false
            })
          } else {
            // 修改
            updateCheckItem(this.form).then(res => {
              // 提示
              if (res.code === 200) this.msgSuccess(res.msg)
              else this.msgError(res.msg)
              // 刷新一下下
              this.getcheckItemTableList()
              // 关闭加载遮罩
              this.loading = false
              // 关闭遮罩
              this.open = false
            }).catch(() => {
              this.loading = false
            })
          }
        } else {
          this.loading = false
          return false
        }
      })
    }
  }
}
</script>
